/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package validators;

import facades.UsuarioFacade;
import java.util.Locale;
import java.util.ResourceBundle;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.faces.application.FacesMessage;
import javax.faces.bean.RequestScoped;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;
import javax.inject.Inject;
import javax.inject.Named;

/**
 *
 * @author damfercer
 */
@Named
@RequestScoped
public class LoginNombreUsuarioValidator implements Validator{
    @Inject
    private UsuarioFacade usuarioFacade;
    
    @Override
    public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException {
        Locale locale=context.getViewRoot().getLocale();
        ResourceBundle rb = ResourceBundle.getBundle("messages", locale);;
        String nombreUsuario=value.toString();
        if(!usuarioFacade.estaRegistrado(nombreUsuario)){
            String emailInUseMsn=rb.getString("UsuarioNoExiste");
            FacesMessage msg =new FacesMessage(emailInUseMsn,emailInUseMsn);
            msg.setSeverity(FacesMessage.SEVERITY_ERROR);
            throw new ValidatorException(msg);
        }
        
    }
    
}
